<!--
 * @Author: CNN 969682281@qq.com
 * @Date: 2023-03-07 14:03:46
 * @LastEditors: CNN 969682281@qq.com
 * @LastEditTime: 2023-06-30 15:11:45
 * @FilePath: \newMould_BHZ\src\views\WaterManage\analyse\percentage.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <!-- 水稳---统计报表----超标统计 -->

  <div class="department" id="department">
    <!-- 左侧部门管理 -->
    <div class="structureTree" v-if="treelength">
      <div class="structureTree_title">
        <div>组织机构</div>
        <div>
          <!-- <el-checkbox v-model="checked" @change="see()">查看下级</el-checkbox> -->
        </div>
      </div>
      <tree
        :treeData="treeData"
        :checkData="checkData"
        nodeKey="fCompanyid"
        @getTreeInfo="handleNodeClick"
        :defaultProps="defaultProps"
      >
      </tree>
    </div>
    <!-- 右侧 -->
    <div class="department_right">
      <!-- 机构名称 -->
      <el-breadcrumb :title="showName"></el-breadcrumb>
      <!-- 顶部导航栏 -->
      <div class="mechanism_nav">
        <!-- 左侧 -->
        <div class="mechanism_nav_left" style="width: 100%">
          <el-date-picker
            @change="selectTime"
            v-model="value2"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
          <el-select
            class="selectBhz"
            style="width: 160px"
            v-model="getList.machineId"
            placeholder="拌和机"
            @change="yj_searchForm(getList)"
          >
            <el-option
              v-for="v in bhjDataList"
              :key="v.fItemName"
              :label="v.fItemName"
              :value="v.fItemValue"
            >
            </el-option>
          </el-select>
          <el-select
            class="selectBhz"
            style="width: 160px"
            v-model="getList.machineId"
            placeholder="拌和站"
            @change="yj_searchForm(getList)"
          >
            <el-option
              v-for="v in bhjDataList"
              :key="v.fItemName"
              :label="v.fItemName"
              :value="v.fItemValue"
            >
            </el-option>
          </el-select>
          <!-- <el-button
              type="primary"
              icon="el-icon-search"
              size="small"
              @click="yj_searchForm(getList)"
              >查询
            </el-button> -->
        </div>
        <!-- 顶部按钮 -->
        <div class="print"></div>
      </div>

      <!-- 表格 -->
      <div class="text_table">
        <div class="" style="width: 100%; height: 100%"></div>
      </div>
    </div>
  </div>
</template>

<script>
const typeOptions = [
  { name: "水", label: "0" },
  { name: "水泥1", label: "1" },
  { name: "水泥2", label: "2" },
  { name: "骨料1", label: "3" },
  { name: "骨料2", label: "4" },
  { name: "骨料3", label: "5" },
  { name: "骨料4", label: "6" },
  { name: "骨料5", label: "7" },
  { name: "矿粉", label: '"8' },
  { name: "粉煤灰", label: "9" },
  { name: "外加剂1", label: "10" },
  { name: "外加剂2", label: "11" },
];

// const typeOptions = ['水', '0水泥1', '水泥2', '骨料1', '骨料2', '骨料3', '骨料4', '骨料5', '矿粉', '粉煤灰', '外加剂1', '外加剂2'];
import tree from "@/components/public/waManage_tree"; //左侧机构树
import elBreadcrumb from "@/components/public/el-breadcrumb.vue"; //面包屑
// import selects from "@/components/public/el-select.vue"

import {
  getUserCompanyTreeList, //左侧机构树接口
  // getCensusList2, //主页面列表接口
  // getStationmachineByfMachineid,
  getStationmachineList, //拌合站列表
} from "@/api/request.js";

export default {
  name: "overstep",
  data() {
    return {
      name: "overstep", //按钮配置页面代号
      // yj_view: null, //查看按钮是否显示
      formData: {}, //新增编辑向后台传递对象
      pickerOptions: {
        //主页面日期筛选 带快捷选项
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              console.log(start);
              start.setTime(start.getTime());
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "昨天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              console.log(start);
              start.setTime(start.getTime() - 3600 * 1000 * 24);
              end.setTime(end.getTime() - 3600 * 1000 * 24);

              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一年",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 365);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },

      level: [],
      bhzDataList: [], //拌合站
      value2: "", //主页面日期筛选绑定值
      highlightBd: true, //左侧机构树停留背景色
      treelength: false, //左侧树显示隐藏
      checked: true, //查看下级按钮
      showName: "未选择公司",
      fId: null, //点击当前列表数据fId
      listLoading: false, //列表数据加载loading
      // 列表给后端传参
      getList: {
        companyId: "", // 施工标段id
        startDates: "", // 开始时间
        endDates: "", // 截至时间

        machineId: "", // 拌和机编号
        enCode: "",
      },
      companyTree: "", //所有数据
      treeData: [], //左侧树状图数据

      // 左侧机构树
      defaultProps: {
        children: "children",
        label: "fShortname",
      },
      checkData: [], //树状图默认选中id
      userInfo: {}, //登录用户
      treeText: {
        //树状图参数
        companyId: null,
        userId: null,
        text: null,
      },
      tableData: [],

      types: typeOptions,
      bhjDataList: [],
    };
  },
  components: {
    tree, //左侧组织机构数
    elBreadcrumb, //面包屑
  },
  mounted() {
    this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
    console.log(
      "列表",
      JSON.parse(sessionStorage.getItem("waterManage_getLists"))
    );
    if (JSON.parse(sessionStorage.getItem("waterManage_getLists"))) {
      var data = JSON.parse(sessionStorage.getItem("waterManage_getLists"));
      this.getList.companyId = data.fCompanyid; // 施工标段id
      this.getList.startDates = data.startDates; // 开始时间
      this.getList.endDates = data.endDates; // 截至时间
      this.getList.machineId = data.machineId; // 拌和机编号
    }
    if (JSON.parse(sessionStorage.getItem("waterManage_treeCheck"))) {
      var id = JSON.parse(sessionStorage.getItem("waterManage_treeCheck"));
      this.checkData = [id.fCompanyid];
    }

    // window.sessionStorage.setItem(
    //         "checkData",
    //         JSON.stringify(e)
    // );
    this.getTree();
    this.btnRole(this.name);
    this.getDate();
  },

  methods: {
    btnRole(name) {
      // 按钮权限
      var btnList = [];
      var ModuleList = JSON.parse(sessionStorage.getItem("ModuleList"));
      if (ModuleList.length > 0) {
        ModuleList.map((item) => {
          if (item.fEncode == name) {
            btnList = item.modulebuttonList;
          }
        });
        var btnStr = [];
        btnList.map((item) => {
          btnStr.push(item.fEncode);
        });
        // this.yj_view = btnStr.includes("yj_view");
      } else {
        // this.yj_view = true;
      }
    },
    // 左侧树结构
    getTree() {
      this.treeText = {
        companyId: this.userInfo.companyId,
        userId: this.userInfo.userId,
        text: "2", // 0其他  1 砼拌合站 2水稳 3沥青
      };
      getUserCompanyTreeList(this.treeText)
        .then((res) => {
          if (res.errcode == 0) {
            var companyTree = res.data.userCompanyTreeList;
            this.treeData = this.tranListToTreeData(companyTree);
            if (this.treeData[0].children == undefined) {
              this.treelength = false; //左侧组织机构是否显示
            } else {
              this.treelength = true;
            }
            if (this.checkData[0]) {
              this.showName = JSON.parse(
                sessionStorage.getItem("waterManage_treeCheck")
              ).fShortname;

              this.getList.companyId = this.checkData[0];
            } else {
              this.showName = this.treeData[0].fShortname; //当前点击组织机构名称
              this.checkData[0] = this.treeData[0].fCompanyid;
              this.getList.companyId = this.treeData[0].fCompanyid; //当前点击组织机构ID
            }
            // this.getList.companyId = this.treeData[0].fCompanyid;//当前点击组织机构ID
            this.mixingPlantList(this.getList);
            this.yj_searchForm(this.getList); // 调用主页面列表接口
            // 列表
          } else {
            this.$message.error(res.msg);
            // this.$message.error("获取组织机构失败");
            return false;
          }
        })
        .catch(() => {
          return false;
        });
    },
    tranListToTreeData(data) {
      //处理树状图数据
      let result = [];
      if (!Array.isArray(data)) {
        return result;
      }
      // 清除已存在的children 重新赋值
      data.forEach((item) => {
        delete item.children;
      });

      let map = {};
      data.forEach((item) => {
        map[item.fCompanyid] = item;
      });

      data.forEach((item) => {
        let parent = map[item.fParentid];
        if (parent) {
          (parent.children || (parent.children = [])).push(item);
        } else {
          result.push(item);
        }
      });
      return result;
    },
    // 点击左侧机构树
    handleNodeClick(data) {
      this.showName = data.fShortname; //顶部显示机构名
      this.getList.companyId = data.fCompanyid;
      this.mixingPlantList(this.getList);
      var time = new Date();
      const y = time.getFullYear();
      const m = (time.getMonth() + 1 + "").padStart(2, "0");
      const d = (time.getDate() + "").padStart(2, "0");
      const hh = (time.getHours() + "").padStart(2, "0");
      const mm = (time.getMinutes() + "").padStart(2, "0");
      const ss = (time.getSeconds() + "").padStart(2, "0");
      var date = new Date();
      var base = Date.parse(date); // 转换为时间戳
      var oneDay = 24 * 3600 * 1000;
      var daytimeArr = [];
      for (var i = 1; i < 7; i++) {
        //前七天的时间
        var now = new Date((base -= oneDay));
        var myear = now.getFullYear();
        var month = now.getMonth() + 1;
        var mday = now.getDate();
        daytimeArr.push(
          [
            myear,
            month >= 10 ? month : "0" + month,
            mday >= 10 ? mday : "0" + mday,
          ].join("-")
        );
      }

      this.getList.startDates = daytimeArr[5] + " " + `00:00:00`; // 开始时间
      this.getList.endDates = `${y}-${m}-${d} ${hh}:${mm}:${ss}`; // 截至时间

      this.getList.machineId = ""; // 拌和机编号

      this.value2 = [
        daytimeArr[5] + " " + "00:00:00",
        `${y}-${m}-${d} ${hh}:${mm}:${ss}`,
      ];
      this.yj_searchForm(this.getList);
    },
    // 获取列表数据
    yj_searchForm(data) {
      // this.listLoading = true;
      console.log(data);
      // window.sessionStorage.setItem(
      //   "waterManage_getLists",
      //   JSON.stringify(data)
      // );
    },

    // 获取拌合站数据
    mixingPlantList(datas) {
      console.log(datas);
      var data = {
        companyId: datas.fCompanyid, // 施工标段id
        modelType: "1", // 类型
        stationType: "2", // 拌合站类型
      };
      this.bhzDataList = [];
      getStationmachineList(data)
        .then((res) => {
          if (res.errcode == 0) {
            var arr = res.data.stationmachineList;
            this.bhzDataList = [
              {
                fItemName: "",
                fItemValue: "",
              },
            ];
            arr.map((item) => {
              this.bhzDataList.push({
                fItemName: item.fMachineid,
                fItemValue: item.fMachineid,
              });
            });
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch((error) => {
          this.$message.error(error.msg);
        });
    },
    // clickLegend(e) {
    //   window.sessionStorage.setItem("checkData", JSON.stringify(e));
    // },
    // checkChange(e) {
    //   window.sessionStorage.setItem("checkData", JSON.stringify(e));
    //   this.yj_searchForm(this.getList);
    // },

    // 时间间隔
    // selectTime() {
    //   this.getList.startDates = this.value2[0] + " " + "00:00:00"; // 开始时间
    //   this.getList.endDates = this.value2[1] + " " + "23:59:59"; // 截至时间

    //   this.xlsxName =
    //     this.value2[0] + "至" + this.value2[1] + "混凝土拌和站记录数据";
    //   this.yj_searchForm(this.getList);
    // },
    selectTime(e) {
      if (e[0] != "" && e[1] != "") {
        this.getList.startDates = e[0] + " " + "00:00:00"; // 开始时间
        this.getList.endDates = e[1] + " " + "23:59:59"; // 截至时间
      } else {
        this.getList.startDates = ""; // 开始时间
        this.getList.endDates = ""; // 截至时间
      }
      this.yj_searchForm(this.getList);
    },

    getDate() {
      var time = new Date();
      const y = time.getFullYear();
      const m = (time.getMonth() + 1 + "").padStart(2, "0");
      const d = (time.getDate() + "").padStart(2, "0");
      const hh = (time.getHours() + "").padStart(2, "0");
      const mm = (time.getMinutes() + "").padStart(2, "0");
      const ss = (time.getSeconds() + "").padStart(2, "0");
      var date = new Date();
      var base = Date.parse(date); // 转换为时间戳
      var oneDay = 24 * 3600 * 1000;
      var daytimeArr = [];
      for (var i = 1; i < 7; i++) {
        //前七天的时间
        var now = new Date((base -= oneDay));
        var myear = now.getFullYear();
        var month = now.getMonth() + 1;
        var mday = now.getDate();
        daytimeArr.push(
          [
            myear,
            month >= 10 ? month : "0" + month,
            mday >= 10 ? mday : "0" + mday,
          ].join("-")
        );
      }
      // this.getList.startDates = `2020-01-01 00:00:00`// 开始时间

      // this.getList.endDates = `${y}-${m}-${d} ${hh}:${mm}:${ss}`// 截至时间
      // this.value2 = ['2020-01-01 00:00:00', `${y}-${m}-${d} ${hh}:${mm}:${ss}`]
      if (JSON.parse(sessionStorage.getItem("waterManage_getLists"))) {
        var data = JSON.parse(sessionStorage.getItem("waterManage_getLists"));
        this.getList.startDates = data.startDates; // 开始时间
        this.getList.endDates = data.endDates; // 截至时间
        this.getList.machineId = data.machineId; // 拌和机编号
        this.value2 = [this.getList.startDates, this.getList.endDates];
        this.xlsxName =
          this.getList.startDates +
          "至" +
          this.getList.endDates +
          "混凝土拌和站记录数据";
      } else {
        this.getList.startDates = daytimeArr[5] + " " + `00:00:00`; // 开始时间
        this.getList.endDates = `${y}-${m}-${d} ${hh}:${mm}:${ss}`; // 截至时间
        this.value2 = [
          daytimeArr[5] + " " + "00:00:00",
          `${y}-${m}-${d} ${hh}:${mm}:${ss}`,
        ];
        this.xlsxName =
          daytimeArr[5] + "至" + `${y}-${m}-${d}` + "混凝土拌和站记录数据";
      }
    },
  },
};
</script>

<style scoped>
.printTables {
  background: #fff;
  display: flex;
  justify-content: end;
  padding: 5px;
  margin-top: 10px;
}

.dialogBody {
  padding: 3px 20px;
  height: 80%;
  overflow-y: auto;
}

.baseTitle {
  /* border: 1px solid #0c204d; */
  border-bottom: 1px solid #0c204d;

  height: 30px;
  line-height: 30px;
  position: relative;
  padding-left: 15px;
}

.baseTitle::after {
  display: block;
  content: "";
  height: 30px;
  width: 5px;
  position: absolute;
  left: 0;
  top: 0;
  background: #0c204d;
}

.materialCharts {
  height: calc(100% - 110px);
  width: 100%;
}

.selectcheckbox {
  margin-left: 15px;
  height: 35px;
  margin-top: -5px;
}
</style>
